package nl.boelen.game.entities;

import java.net.InetAddress;
import nl.boelen.game.client.Client;
import nl.boelen.game.handler.KeyboardHandler;
import nl.boelen.game.handler.MouseHandler;
import nl.boelen.game.net.packet.Packet;
import nl.boelen.game.net.packet.Packet31Movement;

public class PlayerMP extends Player {

    public InetAddress ip;
    public int port;

    private byte newMovingDirection;
    private int gotoX = x;
    private int gotoY = y;

    public PlayerMP(Client game, int x, int y, KeyboardHandler keyboard, MouseHandler mouse, String userName, InetAddress ip, int port,
            String uniqueKey, int shirtColor, int movingDirection) {
        super(game, x, y, keyboard, userName, uniqueKey, shirtColor, 100);
        this.ip = ip;
        this.port = port;
        this.newMovingDirection = (byte) movingDirection;
    }

    public PlayerMP(Client game, int x, int y, String userName, InetAddress ip, int port, String uniqueKey, int shirtColor,
            int movingDirection, int maxhp) {
        super(game, x, y, null, userName, uniqueKey, shirtColor, maxhp);
        this.ip = ip;
        this.port = port;
        this.newMovingDirection = (byte) movingDirection;
    }

    @Override
    public void tick() {
        if (!isClient) {
            if (gotoX != x || gotoY != y) {
                if (x != gotoX) {
                    if (x > gotoX) {
                        mustDecreaseX = true;
                    } else {
                        mustIncreaseX = true;
                    }
                }

                if (y != gotoY) {
                    if (y > gotoY) {
                        mustDecreaseY = true;
                    } else {
                        mustIncreaseY = true;
                    }
                }
            } else {
                movingDirection = newMovingDirection;
            }
        }
        super.tick();
    }

    public void handleMovement(Packet packet) {
        this.gotoX = ((Packet31Movement) packet).getX();
        this.gotoY = ((Packet31Movement) packet).getY();
        this.newMovingDirection = ((Packet31Movement) packet).getMovingDirection();
    }
}
